#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,a[500],x,y,ans,vis[500];
vector<int> to[500];
int read(){
    int s=0;
    char ch=getchar(),last=' ';
    while(ch<'0'||ch>'9')
        last=ch,ch=getchar();
    while(ch>='0'&&ch<='9')
        s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
    return last=='-'?-s:s;
}
void solve(int x,int y){
	if(x>n){
		ans=max(ans,y);
		return;
	}
	for(int i=1;i<=n;i++)
		if(!vis[i]){
			vis[i]=1;
			for(auto v:to[i])
				if(!vis[v])
					a[v]+=a[i];
			solve(x+1,y+a[i]);
			for(auto v:to[i])
				if(!vis[v])
					a[v]-=a[i];
			vis[i]=0;
		}
}
signed main(){
    freopen("light.in","r",stdin);
    freopen("light.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
	}
    int bj=1;
    for(int i=1;i<n;i++){
        x=read(),y=read();
        to[x].push_back(y);
        to[y].push_back(x);
        if(x!=1)
        	bj=0;
    }
    if(n<=10){
        solve(1,0);
        printf("%lld\n",ans);
        return 0;
    }
    int res=0,tmp=0;
    for(int i=1;i<=n;i++)
    	res+=tmp+a[i],tmp+=a[i];
    ans=max(ans,res);
    res=0,tmp=0;
    for(int i=n;i;i--)
    	res+=tmp+a[i],tmp+=a[i];
    ans=max(ans,res);
    printf("%lld\n",ans);
    return 0;
}
